﻿namespace KinonekoSoftware.CodariaDB.Documents.Index
{
    [DebuggerDisplay("{Name}-{Id}-{Type}")]
    [Notion]
    [Glossary]
    public sealed partial class InstanceCache : _Lockable, IEquatable<InstanceCache>, INotion
    {

        private DateTime _timeOfModified;
        private bool     _removable;
        private string   _cover;

        public string Cover
        {
            get => _cover;
            set => SetValue(ref _cover, value);
        }

        public bool Removable
        {
            get => _removable;
            set => SetValue(ref _removable, value);
        }

        public bool UseCustomType { get; init; }

        /// <summary>
        /// 自定义分类
        /// </summary>
        public string CustomType { get; init; }
        
        /// <summary>
        /// Per-Space 
        /// </summary>
        [BsonIgnore]
        public string BelongID { get; set; }

        /// <summary>
        /// 二次分类
        /// </summary>
        public int ExtraType { get; init; }

        public DateTime TimeOfModified
        {
            get => _timeOfModified;
            set => SetValue(ref _timeOfModified, value);
        }

        /// <summary>
        /// 创建时间
        /// </summary>
        public required DateTime TimeOfCreated { get; init; }

        /// <summary>
        /// 主分类
        /// </summary>
        public required InstanceType Type { get; init; }

        /// <summary>
        /// 获得主分类的名字
        /// </summary>
        /// <returns>返回主分类的名字</returns>
        public string GetTypeName()
        {
            if (UseCustomType)
            {
                return CustomType;
            }

            return LanguageManager.GetEnum(Type);
        }
        
        
        string INotion.Id   => Id;
        string INotion.Name => Name;

        public bool Equals(InstanceCache other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Id == other.Id;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((InstanceCache)obj);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }
}